package main;

import java.awt.Container;
import java.awt.Rectangle;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;


public class InsereAlteraFormula extends JFrame{
	private static final long serialVersionUID = 1L;
	private Container cp;
	private JButton cancelar, confirmar, somar, subtrair, multiplicar, dividir, abreParentesis, fechaParentesis, factorial, pi, xquadrado, raiz2, percentagem;
	private JTextArea display;
	private TextField formula, nomebtn;
	private JLabel infoNamebtn;
	private Modelo _modelo;
	private int indiceDaFormulaAModificar;
	private InsereAlteraFormula _this;
	
	public InsereAlteraFormula(Modelo modelo, int index, boolean novaFormula) {
		//if boolean true entao é uma nova formula, senao é o alteraFormula
		_this = this;
		_modelo = modelo;
		indiceDaFormulaAModificar = index;
		setTitle("Inserir/Alterar nova Fórmula");
		setSize(570, 150);
		setLayout(null);
		_modelo.getInterfacePrincipal().setEnabled(false);
		InicilizaComponents();
		SetPosition();
		addComponents();
		addListeners();
		if ( ! novaFormula ) {
			formula.setText("falta completar no construtor");
		}
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		setVisible(true);
		
	}
	private void addComponents() {
		cp.add(display);
		cp.add(formula);
		cp.add(nomebtn);
		cp.add(infoNamebtn);
		cp.add(cancelar);
		cp.add(confirmar);
		cp.add(somar);
		cp.add(subtrair);
		cp.add(multiplicar);
		cp.add(dividir);
		cp.add(abreParentesis);
		cp.add(fechaParentesis);
		cp.add(pi);
		cp.add(percentagem);
		cp.add(factorial);
		cp.add(xquadrado);
		cp.add(raiz2);
		
		
	}
	private void SetPosition() {
		display.setBounds(new Rectangle(5, 5, 335, 30));
		cancelar.setBounds(345, 6, 100, 30);
		confirmar.setBounds(450, 6, 100, 30);
		formula.setBounds(new Rectangle(5, 40, 335, 30));
		infoNamebtn.setBounds(340, 40, 100, 30);
		nomebtn.setBounds(450, 40, 100, 30);
		
		somar.setBounds(5, 75, 45, 30);
		subtrair.setBounds(55, 75, 45, 30);
		multiplicar.setBounds(105,  75, 45, 30);
		dividir.setBounds(155, 75, 45, 30);
		abreParentesis.setBounds(205, 75, 45, 30);
		fechaParentesis.setBounds(255, 75, 45, 30);
		factorial.setBounds(305, 75, 45, 30);
		pi.setBounds(355, 75, 45, 30);
		xquadrado.setBounds(405, 75, 45, 30);
		raiz2.setBounds(455, 75, 45, 30);
		percentagem.setBounds(505, 75, 45, 30);
	}
	private void InicilizaComponents() {
		cp=getContentPane();
		display=new JTextArea();
		display.setEnabled(false);
		formula= new TextField("");
		nomebtn=new TextField();
		infoNamebtn=new JLabel("Nome da formula: ");
		cancelar=new JButton("Cancelar");
		confirmar=new JButton("Confirmar");
		somar=new JButton("+");
		subtrair=new JButton("-");
		multiplicar=new JButton("*");
		dividir=new JButton("/");
		abreParentesis=new JButton("(");
		fechaParentesis=new JButton(")");
		factorial=new JButton("!");
		pi=new JButton("π");
		xquadrado=new JButton("x²");
		raiz2=new JButton("√");
		percentagem=new JButton("%");
	}
	
	private void addListeners(){
		cancelar.addActionListener(new cancelarListener());
		confirmar.addActionListener(new confirmarListener());
		somar.addActionListener(new somarListener());
		subtrair.addActionListener(new subtrairListener());
		multiplicar.addActionListener(new multiplicarListener());
		dividir.addActionListener(new dividirListener());
		abreParentesis.addActionListener(new abreParentesisListener());
		fechaParentesis.addActionListener(new fechaParentesisListener());
		factorial.addActionListener(new factorialListener());
		pi.addActionListener(new piListener());
		xquadrado.addActionListener(new xquadradoListener());
		raiz2.addActionListener(new raiz2Listener());
		percentagem.addActionListener(new percentagemListener());
	}

	public class cancelarListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			_modelo.getInterfacePrincipal().setEnabled(true);
			_this.dispose();
		}
    }
	public class confirmarListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			/*cria objecto*/
						
			//se foi criado sem erros
			//	se nome valido
			formula.getText();
			//		modifica a memoria para guardar o objecto no local correspondente
			//		guarda a memoria em ficheiro
			//**************************************************************
		}
    }
	public class somarListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			formula.setText(formula.getText()+'+');
		}
    }
	public class subtrairListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			formula.setText(formula.getText()+'-');
		}
    }
	public class multiplicarListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			formula.setText(formula.getText()+'*');
		}
    }
	public class dividirListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			formula.setText(formula.getText()+'/');
		}
    }
	public class abreParentesisListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			formula.setText(formula.getText()+'(');
		}
    }
	public class fechaParentesisListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			formula.setText(formula.getText()+')');
		}
    }
	public class factorialListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			formula.setText(formula.getText()+'!');
		}
    }
	public class piListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			formula.setText(formula.getText()+'π');
		}
    }
	public class xquadradoListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			formula.setText(formula.getText()+"^2");
		}
    }
	public class raiz2Listener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			formula.setText(formula.getText()+"√(");
		}
    }
	public class percentagemListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			formula.setText(formula.getText()+'%');
		}
    }
}
